package com.hnkywl.sts.dao.zjgl;

/**
 * Created by lw on 2015/11/10.
 */

import com.hnkywl.sts.entity.zjgl.Xjyh;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.*;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.zjgl
 * 类描述:    异议赔付dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 * Created by lw on 2015/11/4.
 */
@Repository
public class XjyhDao extends SimpleHibernateTemplate<Xjyh, Long> {

    /**
     * 查询
     *
     * @param htbm     合同编号
     * @param pageNo
     * @param rowsSize
     * @return
     */
    public Page<Map> findAllXjyh(String htbm, int pageNo, int rowsSize) {
        Page<Xjyh> page = new Page<Xjyh>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(rowsSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("cght", "cght", CriteriaSpecification.INNER_JOIN)
                .createAlias("kh", "kh", CriteriaSpecification.INNER_JOIN)
                .createAlias("czr", "czr", CriteriaSpecification.INNER_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("id").as("id"))
                                .add(Property.forName("cght.htbm").as("htbm"))
                                .add(Property.forName("kh.qc").as("kh"))
                                .add(Property.forName("xsje").as("xsje"))
                                .add(Property.forName("yhje").as("yhje"))
                                .add(Property.forName("czr.xm").as("czr.xm"))
                                .add(Property.forName("bz").as("bz"))
                                .add(Property.forName("syzt").as("syzt"))
                ).setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(htbm)) {
            criteria.add(Property.forName("cght.htbm").like(htbm.toUpperCase(), MatchMode.ANYWHERE));
        }
        Page list = this.findByCriteria(page, criteria);
        return list;
    }

    /**
     * 下拉列表框查询
     * @param xjyhId
     * @return
     */
    public List<Map> findXsjeByHtid(Long xjyhId) {
        String select = "select sum(t.HSJ) as hsj\n" +
                "  from T_BIZ_HTMX t\n" +
                "  left join t_biz_ht ht\n" +
                "    on ht.id = t.ht_id\n" +
                " where t.HT_ID = " + xjyhId + " and ht.type='cght'\n" +
                "   and t.deleted != 1";
        SQLQuery query = getSession().createSQLQuery(select);
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return query.list();
    }
}
